<?php

!defined('IN_SYSTEM') && exit('Access denied');

class permControlPage extends commonControlPage {

    public function menuModule() {
        if (POST && AJAX) {
            $type = $_POST['type'];

            if ('edit' == $type) {
                $id = (int)$_POST['wparam'];
                $field = $_POST['field'];
                $value = $_POST['val'];

                if(($field == 'sort' OR $field == 'parent_id') && !preg_match('/[0-9]+/',$value)) $this->outputJson(0,'必须为数字',$_POST);

                if ('parent_id' == $field && '0' != $value) {
                    $menu = $_ENV['db']->fetch("SELECT * FROM `admincp_menu` WHERE `menu_id` = '{$value}'");
                    if (!$menu) $this->outputJson(-1,'指定的父级顶级菜单不存在');
                }

                $_ENV['db']->runSql("UPDATE `admincp_menu` SET `{$field}` = '{$value}' WHERE `menu_id` = '{$id}'");

                $this->outputJson(1,'操作成功');
            } elseif ('delete' == $type) {
                $menu_id = (int)$_POST['id'];
                $_ENV['db']->runSql("DELETE FROM `admincp_menu` WHERE `menu_id` = '{$menu_id}'");
                $this->outputJson(1,'操作成功');
            } elseif('add' == $type) {
                $name = $_POST['name'];
                $parent_id = (int)$_POST['parent_id'];
                $sort = (int)$_POST['sort'];

                if (!$name) $this->outputJson(-1,'请填写名称');
                
                if ('0' != $parent_id) {
                    $menu = $_ENV['db']->fetch("SELECT * FROM `admincp_menu` WHERE `menu_id` = '{$parent_id}' AND `parent_id` = '0'");
                    if (!$menu) $this->outputJson(-2,'指定的父级菜单非顶部菜单或指定的菜单不存在');
                }

                $_ENV['db']->runSql("INSERT INTO `admincp_menu` (`name`,`parent_id`,`sort`) VALUES ('{$name}','{$parent_id}','{$sort}')");

                $this->outputJson(1,'操作成功');
            }

            $this->outputJson(-404,'非法操作');
        }

        $where = '';
        switch($_GET['mtype']) {
            case '2':
                $where = 'WHERE `parent_id` != \'0\'';
            break;

            case '1':
                $where = 'WHERE `parent_id` = 0';
            break;
        }

        $menus = $_ENV['db']->fetchAll("SELECT * FROM `admincp_menu` {$where} ORDER BY `sort` DESC");
        $this->view->menus = $menus;
        $this->showMainPage('admincp/menu_manage');
    }

    public function node_relation_menuModule() {
        $menu_id = (int)$_GET['menu_id'];
        $menu = $_ENV['db']->fetch("SELECT * FROM `admincp_menu` WHERE `menu_id` = '{$menu_id}' AND `parent_id` <> 0");
        if(!$menu) $this->showMessage('菜单对象不存在',-404);

        if (POST && AJAX) {
            $node_id = implode(',',(array)$_POST['node_id']);
            if(!$node_id) $node_id = '0';
            $_ENV['db']->runSql("UPDATE `admincp_node` SET `menu_group_id` = '{$menu_id}' WHERE `node_id` IN ({$node_id})");
            $this->outputJson(1,'操作成功');
        }

        $node_rowset = $_ENV['db']->fetchAll("SELECT n.*,m.name as menu_name,m.menu_id FROM `admincp_node` as n LEFT JOIN `admincp_menu` as m ON n.menu_group_id = m.menu_id");

        $nodes = array();
        foreach((array)$node_rowset as $node) {
            $nodes[$node['menu_name']][] = $node;
        }
        ksort($nodes);

        $this->view->menu = $menu;
        $this->view->nodes = $nodes;
        $this->showMainPage('admincp/node_relation_menu');
    }

    public function nodeModule() {
        if (POST && AJAX) {
            $type = $_POST['type'];

            if ('edit' == $type) {
                $wparam = (int)$_POST['wparam'];
                $value = $_POST['val'];
                $field = $_POST['field'];
                
                if ('sort' == $field && !is_numeric($value)) $this->outputJson(0,'排序必须为数字');

                $_ENV['db']->runSql("UPDATE `admincp_node` SET `{$field}` = '{$value}' WHERE `node_id` = '{$wparam}'");
                $this->outputJson(1,'操作成功');
            } elseif ('yesno' == $type) {
                $node_id = (int)$_POST['node_id'];
                $node = $_ENV['db']->fetch("SELECT * FROM `admincp_node` WHERE `node_id` = '{$node_id}'");
                if (!$node) $this->outputJson(-404,'非法操作');
                $_ENV['db']->runSql("UPDATE `admincp_node` SET `hidden` = '".($node['hidden'] ? '0' : '1')."' WHERE `node_id` = '{$node_id}'");
                $this->outputJson(1,'操作成功','./res/image/'.($node['hidden'] ? 'yes' : 'no').'.gif');
            } elseif ('delete' == $type) {
                $node_id = (int)$_POST['node_id'];
                $_ENV['db']->runSql("DELETE FROM `admincp_node` WHERE `node_id` = '{$node_id}'");
                $this->outputJson(1,'操作成功');
            } elseif('add' == $type) {
                $name = $_POST['name'];
                $app = $_POST['app_val'];
                $mod = $_POST['mod_val'];
                $hidden = $_POST['hidden'];

                if ($hidden != 1 && $hidden != 0) $this->outputJson(0,'HIDE Error');

                if (!$name OR !$app OR !$mod) $this->outputJson(-1,'请补全信息');

                $node = $_ENV['db']->fetch("SELECT * FROM `admincp_node` WHERE `app_val` = '{$app}' AND `mod_val` = '{$mod}'");
                if ($node) $this->outputJson(-2,'已经存在的App和Mod');

                $_ENV['db']->runSql("INSERT INTO `admincp_node` (`name`,`app_val`,`mod_val`,`hidden`) VALUES ('{$name}','{$app}','{$mod}','{$hidden}')");

                $this->outputJson(1,'操作成功');
            }

            $this->outputJson(-404,'非法操作');
        }

        $menus = $nodes = array();
        $where = '';
        if(is_numeric($_GET['menu_id'])) {
            $where = 'where m.menu_id = '.$_GET['menu_id'];
        }
        $node_rowset = $_ENV['db']->fetchAll("SELECT n.*,m.name as menu_name,m.menu_id FROM `admincp_node` as n LEFT JOIN `admincp_menu` as m ON n.menu_group_id = m.menu_id {$where} ORDER BY n.sort");
        if ($node_rowset) {
            $nodes =& $node_rowset;
        }

        $menu_rowset = $_ENV['db']->fetchAll('SELECT * FROM `admincp_menu` WHERE `parent_id` != 0 ORDER BY `sort`');
        if($menu_rowset) {
            $menus =& $menu_rowset;
        }

        $this->view->menus = $menus;
        $this->view->nodes = $nodes;
        $this->showMainPage('admincp/node_manage');
    }
}